<script setup>
import { ref, onMounted } from "vue"
import request from '@/api/request'
import { ElMessage } from 'element-plus'

let emits = defineEmits(["ref-table"])

// 初始化表单数据
let processStep = ref({
  processId: '',
  processName: '',
  isEnabled: '0', // 默认启用
  processIllustrate: ''
})

// 自动生成功能相关
const autoGenerate = ref(false)

// 组件挂载时如果开启自动生成则生成编码
onMounted(() => {
  if (autoGenerate.value) {
    generateProcessId()
  }
})

// 生成工序编码
const generateProcessId = () => {
  const timestamp = new Date().getTime().toString()
  return 'PRO' + timestamp.substring(timestamp.length - 8)
}

// 处理自动生成功能开关
const handleAutoGenerate = (val) => {
  if (val) {
    // 开启自动生成
    processStep.value.processId = generateProcessId()
  } else {
    // 关闭自动生成，清空编码
    processStep.value.processId = ''
  }
}

// 重新生成编码
const regenerateCode = () => {
  if (autoGenerate.value) {
    processStep.value.processId = generateProcessId()
  }
}

let onSubmit = () => {
  // 表单验证
  if (!processStep.value.processId) {
    ElMessage.warning('工序编码不能为空')
    return
  }
  
  if (!processStep.value.processName) {
    ElMessage.warning('工序名称不能为空')
    return
  }
  
  request({
    url: '/processStep/insert',
    method: 'post',
    data: {
      ...processStep.value
    }
  }).then(res => {
    if (res.code !== 200) {
      ElMessage.error(res.message || res.data)
    } else {
      ElMessage.success('添加成功')
      emits("ref-table")
      // 如果开启自动生成，成功后生成新的编码
      if (autoGenerate.value) {
        processStep.value.processId = generateProcessId()
        // 其他字段重置
        processStep.value.processName = ''
        processStep.value.processIllustrate = ''
      }
    }
  }).catch(error => {
    ElMessage.error('请求失败: ' + error.message)
  })
}
</script>

<template>
  <div>
    <el-card>
      <template #header>
        <el-form :model="processStep" label-width="120px" class="card_wrap">
         
            <el-form-item label="工序编码" class="c_item_big">
              <div style="display: flex; align-items: center;">
                <el-input 
                  v-model="processStep.processId" 
                  style="width: 150px;"
                  :readonly="autoGenerate"
                  placeholder="请输入工序编码"
                ></el-input>
                <el-switch 
                  v-model="autoGenerate"
                  active-text="自动生成"
                  @change="handleAutoGenerate"
                  style="margin-left: 15px;"
                />
                <el-button 
                  v-if="autoGenerate"
                  @click="regenerateCode"
                  style="margin-left: 10px;"
                  size="small"
                >
                  重新生成
                </el-button>
              </div>
            </el-form-item>
            <el-form-item label="工序名称" class="c_item-big">
              <el-input 
                v-model="processStep.processName" 
                style="width: 150px;"
                placeholder="请输入工序名称"
              ></el-input>
            </el-form-item>
          
          
          <el-form-item label="是否启用" class="c_item">
            <el-radio-group v-model="processStep.isEnabled">
              <el-radio value="0" size="large">是</el-radio>
              <el-radio value="1" size="large">否</el-radio>
            </el-radio-group>
          </el-form-item>
          
          <el-form-item label="说明" class="c_item_big">
            <el-input 
              v-model="processStep.processIllustrate" 
              style="width:800px"
              placeholder="请输入内容"
              type="textarea"
              :rows="4"
            ></el-input>
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即添加</el-button>
          </el-form-item>
        </el-form>
      </template>
    </el-card>
  </div>
</template>

<style scoped>
.c_item {
  flex: 1;
  margin: 0px 5px 15px 0;
  width: calc((100% - 10px) / 2);
  min-width: calc((100% - 10px) / 2);
  max-width: calc((100% - 10px) / 2);
}

.c_item_big {
  flex: 2;
  margin: 0px 5px 15px 0;
  width: calc((100% - 10px) / 1);
  min-width: calc((100% - 10px) / 1);
  max-width: calc((100% - 10px) / 1);
}

.card_wrap {
  width: 98%;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
}

.form-row {
  display: flex;
  width: 100%;
}
</style>